package com.clusterswisdom.horn.api.tmall.action.item;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.db.DbUtil;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemQuantityUpdateResponse;
import com.taobao.api.response.ItemsInventoryGetResponse;

public class SetItemsNumOfUnsaleAction extends AbstractTmallAction implements HornAction{
	
	/**
	 * 将商家下架商品库存全部修改为20
	 * @author Jin_tian
	 *
	 */
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
			List<Item> items=getListOfItem(param);
			if(setNumOfItems(items)){
				return new HornResponse(true, "成功");
				
			}else{
				return new HornResponse(false, "失败");
				
			}
		}
		
		/**
		 * @param  无
		 * @return 商家仓库中宝贝列表
		 * @throws Exception 
		 */
		public List<Item> getListOfItem(JSONObject param) throws Exception{
			int pageNo = 1;
			long totalNum = -1;
			
			List<Item> items = new LinkedList<Item>();
			do {
				param.put("page_no", pageNo++);
				ItemsInventoryGetResponse response = (ItemsInventoryGetResponse)TmallInstance.getInstance().executeRequest(param, "item", "itemsInventoryGet");
				
				if (totalNum == -1) {
					totalNum = response.getTotalResults();
				}
			
				if(response.getItems() != null){
					items.addAll(response.getItems());
				}
				
			} while (items.size() < totalNum);
			
			return items;
		}
		
		/**
		 * @param 宝贝列表（商品的id不能为空）
		 * @return 全部设置是否成功
		 * @throws SQLException 
		 * @throws Exception 
		 */
		public boolean setNumOfItems(List<Item> items) throws SQLException {
			Connection con = DbUtil.getUtil().getConnection();
			String sql = "select * from itemsforsetting";
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			Map<Long,String> itemOfNotSet = new HashMap<Long, String>(); 
			while(rs.next()){
				itemOfNotSet.put(rs.getLong("itemid"), "true");
			}
			ps.close();
			DbUtil.getUtil().close();
			
			boolean hasError = false;
			JSONObject param=new JSONObject();
			
			for(Item item:items){
				if(itemOfNotSet.get(item.getNumIid()) == null){
					param.put("num_iid", item.getNumIid());
					param.put("quantity","20");
					ItemQuantityUpdateResponse response = null;
					try {
						response = (ItemQuantityUpdateResponse)TmallInstance.getInstance().executeRequest(param, "schema", "itemQuantityUpdate");
					} catch (Exception e) {
						e.printStackTrace();
					}
					hasError = !response.isSuccess();
				}
			}
			return !hasError;
		}
	

}
